package com.mit.wx.util.xml;

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

/**
 * @author 郭文梁
 * <p>${DATE} ${TIME} Create by 郭文梁</p>
 * <p>FOR: XML CDATA节点转换器</p>
 */
public class CDataConverter implements Converter {
    private static final String PREFIX = "<![CDATA[";
    private static final String SUFFIX = "]]>";

    @Override
    public void marshal(Object o, HierarchicalStreamWriter writer, MarshallingContext context) {
        writer.setValue(PREFIX + o + SUFFIX);
    }

    @Override
    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
        return reader.getValue();
    }

    @Override
    public boolean canConvert(Class clazz) {
        return String.class.equals(clazz);
    }
}
